//
//  FQAutoScrollView.swift
//  SwiftProject
//
//  Created by 董駸 on 2023/4/7.
//

import UIKit

class FQAutoScrollView: UIImageView, UITableViewDataSource, UITableViewDelegate {
    let dataArray = [["title" : "石*", "data" : "获得60000额度", "time" : "28分钟前"],
                     ["title" : "鲍* ", "data" : "获得160000额度", "time" : "20分钟前"],
                     ["title" : "徐**", "data" : "获得120000额度", "time" : "18分钟前"],
                     ["title" : "王**", "data" : "获得180000额度", "time" : "10分钟前"],
                     ["title" : "李*", "data" : "获得56000额度", "time" : "8分钟前"],
                     ["title" : "章*", "data" : "获得20000额度", "time" : "5分钟前"],
                     ["title" : "李**", "data" : "获得150000额度", "time" : "5分钟前"],
                     ["title" : "唐*", "data" : "获得100000额度", "time" : "1分钟前"]]
    let tableView : UITableView = UITableView(frame: .zero, style: .plain)
    var timer : Timer?
    var timerInt = 0
    var buttonBlock : (()->Void)?

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(buttonBlock : (()->Void)?) {
        self.init(frame: .zero)
        self.buttonBlock = buttonBlock
        
        image = UIImage(named: "homeBackground2_2")
        isUserInteractionEnabled = true
        
        let titleLb = QMUILabel(font: .mSFont(16), textColor: .color51)
        titleLb.text = "今日申请，审批通过率高"
        addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.top.equalTo(24)
        }
        
        let activateBtn = QMUIButton()
        activateBtn.backgroundColor = .qmui_color(withHexString: "eaf2fe")
        activateBtn.setTitle("立即申请", for: .normal)
        activateBtn.layer.cornerRadius = 15
        activateBtn.clipsToBounds = true
        activateBtn.setTitleColor(.colorMain, for: .normal)
        activateBtn.titleLabel?.font = .mSFont(13)
        activateBtn.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        addSubview(activateBtn)
        
        activateBtn.snp.makeConstraints { make in
            make.centerY.equalTo(titleLb);
            make.right.equalTo(-23);
            make.height.equalTo(30);
            make.width.equalTo(82);
        }
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.rowHeight = 40
        tableView.isScrollEnabled = false
        tableView.separatorStyle = .none
        tableView.register(FQAutoScrollViewCell.self, forCellReuseIdentifier: "cell")
        addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.left.equalTo(titleLb)
            make.right.equalTo(activateBtn)
            make.top.equalTo(titleLb.snp.bottom).offset(20)
            make.bottom.equalTo(-10)
        }
        
        if timer != nil {
            timer?.invalidate()
            timer = nil
        }
        
        timer = Timer.scheduledTimer(withTimeInterval: 1.5, repeats: true, block: { [unowned self] timer in
            self.timerInt += 1;
            if (self.timerInt == self.dataArray.count) {
                self.timerInt = 0;
                self.tableView.scrollToRow(at: IndexPath(row: self.timerInt, section: 0), at: .top, animated: false)
            } else {
                self.tableView.scrollToRow(at: IndexPath(row: self.timerInt, section: 0), at: .top, animated: true)
            }
        })
        RunLoop.main.add(timer!, forMode: .common)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FQAutoScrollViewCell
        cell.dict = dataArray[indexPath.row]
        return cell
    }
    
    @objc func buttonClick() {
        buttonBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        if timer != nil {
            timer!.invalidate()
            timer = nil
        }
    }
}
